home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / PowerPlant / LColorSwatch / LColorSwatch.cp < prev    next >
Encoding:
Text File  |  1996-09-14  |  7.7 KB  |  247 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    File:                        LColorSwatch.cp
  3. //  Version:                1.0 - Feb 08, 1995
  4. //    Author:                    Mike Shields (mshields@inconnect.com)
  5. //                            
  6. //    Copyright ©1996 Mike Shields. All rights reserved.
  7. //    I hereby grant users of LColorSwatch permission to use it (or any modified 
  8. //    version of it) in applications (or any other type of Macintosh software 
  9. //    like extensions -- freeware, shareware, commercial, or other) for free, 
  10. //    subject to the terms that:
  11. //
  12. //        (1)  This agreement is non-exclusive.
  13. //
  14. //        (2)  I, Mike Shields, retain the copyright to the original source code.
  15. //
  16. //    These two items are the only required conditions for use. However, I do have 
  17. //    an additional request. Note, however, that this is only a request, and 
  18. //    that it is not a required condition for use of this code.
  19. //
  20. //        (1) That I be given credit for LColorSwatch code in the copyrights or 
  21. //            acknowledgements section of your manual or other appropriate documentation.
  22. //
  23. //
  24. //    I would like to repeat that this last item is only a request. You are prefectly 
  25. //    free to choose not to do any or all of them.
  26. //    
  27. //        This source code is distributed in the hope that it will be useful,
  28. //        but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. //        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30. // ===========================================================================
  31. //    LColorSwatch.h        <- double-click + Command-D to see class declaration
  32. //
  33. // Originally based on the code by Brian Hill <lorax@msn.fullfeed.com>
  34. #include "LColorSwatch.h"
  35. #include <ColorPicker.h>
  36.  
  37. // ---------------------------------------------------------------------------
  38. //        • CreateFromStream
  39. // ---------------------------------------------------------------------------
  40. // Static function to create a ColorSwatch from the data in a stream
  41. LColorSwatch* LColorSwatch::CreateFromStream(LStream *inStream)
  42. {
  43.     return(new LColorSwatch(inStream));
  44. }
  45.  
  46. // ---------------------------------------------------------------------------
  47. //        • LColorSwatch
  48. // ---------------------------------------------------------------------------
  49. // Default constructor
  50. LColorSwatch::LColorSwatch()
  51. {
  52.     mColor.red = 0x0000;
  53.     mColor.green = 0x0000;
  54.     mColor.blue = 0x0000;
  55. }
  56.  
  57. // ---------------------------------------------------------------------------
  58. //        • LColorSwatch
  59. // ---------------------------------------------------------------------------
  60. // Copy constructor
  61. LColorSwatch::LColorSwatch(const LColorSwatch& inOriginal)
  62.     : LControl(inOriginal)
  63. {
  64.     mColor.red = inOriginal.mColor.red;
  65.     mColor.green = inOriginal.mColor.green;
  66.     mColor.blue = inOriginal.mColor.blue;
  67.     
  68.     mPrompt = inOriginal.mPrompt;
  69. }
  70.  
  71. // ---------------------------------------------------------------------------
  72. //        • LColorSwatch
  73. // ---------------------------------------------------------------------------
  74. // create a color swatch from parameters
  75. LColorSwatch::LColorSwatch(const SPaneInfo &inPaneInfo, MessageT inValueMessage,
  76.                                     Int32 inValue, Int32 inMinValue, Int32 inMaxValue, 
  77.                                     ConstStringPtr inString, RGBColor& inColor)
  78.     : LControl(inPaneInfo, inValueMessage, inValue, inMinValue, inMaxValue)
  79. {
  80.     mColor.red = inColor.red;
  81.     mColor.green = inColor.green;
  82.     mColor.blue = inColor.blue;
  83.     
  84.     mPrompt = inString;
  85. }
  86.  
  87. // ---------------------------------------------------------------------------
  88. //        • LColorSwatch
  89. // ---------------------------------------------------------------------------
  90. // create a ColorSwatch from a stream
  91. LColorSwatch::LColorSwatch(LStream* inStream)
  92.     : LControl(inStream)
  93. {
  94.     inStream->ReadData(&mColor, sizeof(RGBColor));
  95.     inStream->ReadPString(mPrompt);
  96. }
  97.  
  98. // ---------------------------------------------------------------------------
  99. //        • ~LColorSwatch
  100. // ---------------------------------------------------------------------------
  101. // Destructor
  102. LColorSwatch::~LColorSwatch()
  103. {
  104. }
  105.  
  106. // ---------------------------------------------------------------------------
  107. //        • GetDescriptor
  108. // ---------------------------------------------------------------------------
  109. //    Return prompt of a ColorSwatch as a string
  110. StringPtr LColorSwatch::GetDescriptor(Str255 outDescriptor) const
  111. {
  112.     return LString::CopyPStr(mPrompt, outDescriptor);
  113. }
  114.  
  115.  
  116. // ---------------------------------------------------------------------------
  117. //        • SetDescriptor
  118. // ---------------------------------------------------------------------------
  119. //    Set prompt of a ColorSwatch from a string
  120. void LColorSwatch::SetDescriptor(ConstStringPtr inDescriptor)
  121. {
  122.     mPrompt = inDescriptor;
  123. }
  124.  
  125.  
  126. // ---------------------------------------------------------------------------
  127. //        • GetColor
  128. // ---------------------------------------------------------------------------
  129. // get the color currently stored in a color swatch
  130. void LColorSwatch::GetColor(RGBColor& outColor)
  131. {
  132.     outColor.red = mColor.red;
  133.     outColor.green = mColor.green;
  134.     outColor.blue = mColor.blue;
  135. }
  136.  
  137.  
  138. // ---------------------------------------------------------------------------
  139. //        • SetColor
  140. // ---------------------------------------------------------------------------
  141. // set the color currently stored in a color swatch
  142. void LColorSwatch::SetColor(RGBColor& inColor)
  143. {
  144.     mColor.red = inColor.red;
  145.     mColor.green = inColor.green;
  146.     mColor.blue = inColor.blue;
  147. }
  148.  
  149. // ---------------------------------------------------------------------------
  150. //        • HotSpotAction
  151. // ---------------------------------------------------------------------------
  152. //    Take action during mouse down tracking
  153. //
  154. //    ColorSwatches toggle between two states depending on whether the cursor is
  155. // inside the border..
  156. void LColorSwatch::HotSpotAction(Int16 /*inHotSpot*/, Boolean inCurrInside, 
  157.                                         Boolean inPrevInside)
  158. {
  159.     if ( inCurrInside != inPrevInside ) 
  160.     {    
  161.         FocusDraw();
  162.         StColorPenState    thePenState;
  163.         Rect                    frame;
  164.     
  165.         StColorPenState::Normalize();
  166.     
  167.         CalcLocalFrameRect(frame);
  168.         
  169.         if ( inCurrInside == true )
  170.         {
  171.             ::PenSize(2, 2);
  172.         }
  173.         ::EraseRect(&frame);
  174.         ::FrameRect(&frame);
  175.         ::InsetRect(&frame, 3, 3);
  176.         ::RGBForeColor(&mColor);
  177.         ::PaintRect(&frame);
  178.     }
  179. }
  180.  
  181.  
  182. // ---------------------------------------------------------------------------
  183. //        • HotSpotResult
  184. // ---------------------------------------------------------------------------
  185. // Display the color picker dialog and broadcast the fact the data's changed
  186. // if the user selected an new color.
  187. void LColorSwatch::HotSpotResult(Int16 inHotSpot)
  188. {
  189.     Point        where = {-1, -1};
  190.     RGBColor    outColor;
  191.     Boolean colorChosen = ::GetColor(where, mPrompt, &mColor, &outColor);
  192.     
  193.     if ( colorChosen )
  194.     {
  195.         mColor.red = outColor.red;
  196.         mColor.green = outColor.green;
  197.         mColor.blue = outColor.blue;
  198.     }
  199.  
  200.     // Undo Button hilighting
  201.     HotSpotAction(inHotSpot, false, true);
  202.  
  203.     if ( colorChosen && (mValueMessage != cmd_Nothing) ) 
  204.     {
  205.         BroadcastMessage(mValueMessage, (void*)&outColor);
  206.     }
  207. }
  208.  
  209.  
  210. // ---------------------------------------------------------------------------
  211. //        • PointInHotSpot
  212. // ---------------------------------------------------------------------------
  213. // Point is in the frame if it is inside the color swatch in the control.
  214. Boolean LColorSwatch::PointInHotSpot(Point inPoint, Int16 /*inHotSpot*/)
  215. {
  216.     Rect        frame;
  217.     Boolean isInFrame = false;
  218.     
  219.     if ( CalcLocalFrameRect(frame) ) 
  220.     {
  221.         ::InsetRect(&frame, 3, 3);
  222.         
  223.         isInFrame = ::PtInRect(inPoint, &frame);
  224.     }
  225.     
  226.     return isInFrame;
  227. }
  228.  
  229. // ---------------------------------------------------------------------------
  230. //        • DrawSelf
  231. // ---------------------------------------------------------------------------
  232. void LColorSwatch::DrawSelf()
  233. {
  234.     StColorPenState    thePenState;
  235.     Rect                    frame;
  236.  
  237.     StColorPenState::Normalize();
  238.  
  239.     CalcLocalFrameRect(frame);
  240.     
  241.     ::EraseRect(&frame);
  242.     ::FrameRect(&frame);
  243.     ::InsetRect(&frame, 3, 3);
  244.     ::RGBForeColor(&mColor);
  245.     ::PaintRect(&frame);
  246. }
  247.